package com.aglhz.nature.view;




import java.util.List;

import com.aglhz.nature.R;
import com.aglhz.nature.fragment.ClassificationFragment;
import com.aglhz.nature.fragment.HomePageFragment;
import com.aglhz.nature.fragment.MyselfFragment;
import com.aglhz.nature.fragment.ServiceFragment;
import com.aglhz.nature.fragment.ShopCartFragment;
import com.aglhz.nature.utils.ToastUtil;
import com.umeng.socialize.utils.Log;

import butterknife.OnClick;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * 已优化主页面fragment
 * @author wzb
 *
 */
public class MainActivity extends FragmentActivity  implements OnClickListener {
	public final static int TO_FRAG_HOME_TAG = 0;
	public final static int TO_FRAG_CAT_TAG = 1;
	public final static int TO_FRAG_MEMBER_TAG = 2;
	public final static int TO_FRAG_SHOPCART_TAG = 3;
	public final static int TO_FRAG_SERVICE_TAG = 4;
	public final static int TO_CRRENT_TAG = -1;
	
	private long exitTime=2000;
	
	public static MainActivity mainActivity = null;
	
	public static int MAIN_FRAM_TAG = TO_CRRENT_TAG;
	
	private SharedPreferences sharedPreferences; 
	 private FragmentManager mFM = null;
	 private String LoginInfo;
	 private String Login_Tag;
	 private String NORMAL_LogInfo;
	 private String nor_token;
	 private String wx_token;
	 private String token;
	 private int index;

	 
	 private HomePageFragment homePageFragment;
	 private ClassificationFragment classificationFragment;
	 private MyselfFragment myselfFragment;
	 private ShopCartFragment shopCartFragment;
	 private ServiceFragment serviceFragment;
	
	 
	 
	 
	ImageView iv_tab_homepage;
	 ImageView iv_tab_classification;
	 ImageView iv_tab_myself;
	 ImageView iv_tab_shopcart;
	 ImageView iv_tab_service;
	LinearLayout mTab_item_container;
	TextView tv_tab_home;
	 TextView tv_tab_classification;
	 TextView tv_tab_myself;
	 TextView tv_tab_shopcart;
	 TextView tv_tab_service;
	private LinearLayout Lin_homepage,Lin_classification,Lin_myself,Lin_shopcart,Lin_service;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mainActivity=this;
        mFM = getSupportFragmentManager();
      
      //获取sharedPreferences里面的信息,微信
      		sharedPreferences = getSharedPreferences("WX_INFO", Context.MODE_PRIVATE);
      		wx_token=sharedPreferences.getString("token", "");
      		//获取sharedPreferences里面的信息,普通登陆
      		sharedPreferences =getSharedPreferences("Normal_Info",Context.MODE_PRIVATE);
      		nor_token=sharedPreferences.getString("NORMAL_Token", "");
      		if(!(""==wx_token)){
      			token=wx_token;
      		}else{
      			token=nor_token;
      		}
        

        
        Intent intent =getIntent();
        LoginInfo = intent.getStringExtra("LOGININFO");
        Login_Tag= intent.getStringExtra("Login_Tag");
        NORMAL_LogInfo=intent.getStringExtra("NORMAL_LogInfo");
//        JSBack=intent.getStringExtra("JS");
//        ButterKnife.bind(this);
        
        iv_tab_homepage=(ImageView) findViewById(R.id.iv_tab_homepage);
        iv_tab_classification=(ImageView) findViewById(R.id.iv_tab_classification);
        iv_tab_myself=(ImageView) findViewById(R.id.iv_tab_myself);
        iv_tab_shopcart=(ImageView) findViewById(R.id.iv_tab_shopcart);
        iv_tab_service=(ImageView) findViewById(R.id.iv_tab_service);
        mTab_item_container=(LinearLayout) findViewById(R.id.tab_item_container);
        
        tv_tab_home=(TextView) findViewById(R.id.tv_tab_home);
        tv_tab_classification=(TextView) findViewById(R.id.tv_tab_classification);
        tv_tab_myself=(TextView) findViewById(R.id.tv_tab_myself);
        tv_tab_shopcart=(TextView) findViewById(R.id.tv_tab_shopcart);
        tv_tab_service=(TextView) findViewById(R.id.tv_tab_service);
        
        Lin_homepage=(LinearLayout) findViewById(R.id.Lin_homepage);
        Lin_classification=(LinearLayout) findViewById(R.id.Lin_classification);
        Lin_myself=(LinearLayout) findViewById(R.id.Lin_myself);
        Lin_shopcart=(LinearLayout) findViewById(R.id.Lin_shopcart);
        Lin_service=(LinearLayout) findViewById(R.id.Lin_service);
        
        Lin_homepage.setOnClickListener(this);
        Lin_classification.setOnClickListener(this);
        Lin_myself.setOnClickListener(this);
        Lin_shopcart.setOnClickListener(this);
        Lin_service.setOnClickListener(this);
       //微信 Login_Tag
//        if(!TextUtils.isEmpty(JSBack)){
//        	changeHomePage();
//        }else {
        	if(!(null==LoginInfo)||!(null==Login_Tag)||!(null==NORMAL_LogInfo)){
            	changeMyself();
            }else{
            
            	changeHomePage();
            }
//		}
        
       
    }


    @Override
    protected void onResume(){

    	if(TO_FRAG_HOME_TAG==MAIN_FRAM_TAG  ){
    		///
    		changeHomePage();
    		MAIN_FRAM_TAG=TO_CRRENT_TAG;
    	}else if(TO_FRAG_MEMBER_TAG==MAIN_FRAM_TAG){
    		changeMyself();
    		MAIN_FRAM_TAG=TO_CRRENT_TAG;
    	}else if(TO_FRAG_SHOPCART_TAG==MAIN_FRAM_TAG){
    		changeShopCart();
    	}
    	
    	
    	super.onResume();
    }
    
    @Override
	protected void onNewIntent(Intent intent) {
    	//获取sharedPreferences里面的信息,微信
  		sharedPreferences = getSharedPreferences("WX_INFO", Context.MODE_PRIVATE);
  		wx_token=sharedPreferences.getString("token", "");
  		//获取sharedPreferences里面的信息,普通登陆
  		sharedPreferences =getSharedPreferences("Normal_Info",Context.MODE_PRIVATE);
  		nor_token=sharedPreferences.getString("NORMAL_Token", "");
  		if(!(""==wx_token)){
  			token=wx_token;
  		}else{
  			token=nor_token;
  		}
  		
    	List<Fragment> fragments= mFM.getFragments();
    	for (Fragment f:fragments) {
			if (f instanceof MyselfFragment) {
				((MyselfFragment)f).getPresenter().loginCheck(token);
			}else if(f instanceof ShopCartFragment){
				intent =new Intent(this,ShopActivity.class);
				startActivity(intent);
			}
			
		}
		super.onNewIntent(intent);
	}

    

	@Override
	public void onClick(View arg0) {
	switch (arg0.getId()) {
	case R.id.Lin_homepage:
		changeHomePage();
		break;
	case R.id. Lin_classification:		
		changeClassification();
		break;
	case R.id.Lin_myself:
		changeMyself(); 
		break;
	case R.id.Lin_shopcart:
		if(""==token){
			Intent intent=new Intent(MainActivity.this,LoginActivity.class);
			startActivity(intent);
		}else{
			changeShopCart();
		}
		
		break;
	case R.id.Lin_service:
		changeService();
		break;
	default:
		break;
	}
		
	}
//	

	/**
     * change fragement
     */

	

	private void hideFragments(  FragmentTransaction    ft ) {
		
		
		
		if (homePageFragment!=null&&!homePageFragment.isHidden()) {
			
			ft.hide(homePageFragment);
		}
		if (classificationFragment!=null&&!classificationFragment.isHidden()) {
			ft.hide(classificationFragment);
		}
		if (myselfFragment!=null&&!myselfFragment.isHidden()) {
			ft.hide(myselfFragment);
		}
		if (shopCartFragment!=null&&!shopCartFragment.isHidden()) {
			ft.hide(shopCartFragment);
		}
		if (serviceFragment!=null&&!serviceFragment.isHidden()) {
			ft.hide(serviceFragment);
		}
	}

	public void changeHomePage() {
		
	    iv_tab_homepage.setBackgroundResource(R.drawable.homepage_selected);
        iv_tab_classification.setBackgroundResource(R.drawable.classification_normal);
        iv_tab_myself.setBackgroundResource(R.drawable.myself_normal);
        iv_tab_shopcart.setBackgroundResource(R.drawable.shopcart_normal);
    	iv_tab_service.setBackgroundResource(R.drawable.service_normal);
    	tv_tab_home.setTextColor(getResources().getColor(R.color.tab_selected));
    	tv_tab_classification.setTextColor(getResources().getColor(R.color.tab_normal));
    	 tv_tab_myself.setTextColor(getResources().getColor(R.color.tab_normal));
    	 tv_tab_shopcart.setTextColor(getResources().getColor(R.color.tab_normal));
    	 tv_tab_service.setTextColor(getResources().getColor(R.color.tab_normal));
    	 
    	  FragmentTransaction    ft = mFM.beginTransaction();
    	 hideFragments(ft);
		 if(homePageFragment==null){
			 homePageFragment=new HomePageFragment();
			 ft.add(R.id.content_container, homePageFragment);
		 }else {
			ft.show(homePageFragment);
		}
	        
	    
	    	 index=1;
	        ft.commit();
		
	}
	
	public void changeClassification() {
		  FragmentTransaction    ft = mFM.beginTransaction();
		 hideFragments(ft);
		 if(classificationFragment==null){
			 classificationFragment=new ClassificationFragment();
			 ft.add(R.id.content_container, classificationFragment);
		 }else {
			ft.show(classificationFragment);
		}
        iv_tab_homepage.setBackgroundResource(R.drawable.homepage_normal);
        iv_tab_classification.setBackgroundResource(R.drawable.classification_selected);
        iv_tab_myself.setBackgroundResource(R.drawable.myself_normal);
        iv_tab_shopcart.setBackgroundResource(R.drawable.shopcart_normal);
    	iv_tab_service.setBackgroundResource(R.drawable.service_normal);
    	
    	tv_tab_home.setTextColor(getResources().getColor(R.color.tab_normal));
    	tv_tab_classification.setTextColor(getResources().getColor(R.color.tab_selected));
    	 tv_tab_myself.setTextColor(getResources().getColor(R.color.tab_normal));
    	 tv_tab_shopcart.setTextColor(getResources().getColor(R.color.tab_normal));
    	 tv_tab_service.setTextColor(getResources().getColor(R.color.tab_normal));
    	 index=2;
        ft.commit();
		
	}
	
	public void changeMyself() {
		  FragmentTransaction    ft = mFM.beginTransaction();
		 hideFragments(ft);
		 if(myselfFragment==null){
			 myselfFragment=new MyselfFragment();
			 ft.add(R.id.content_container, myselfFragment);
		 }else {
			ft.show(myselfFragment);
		}
        iv_tab_homepage.setBackgroundResource(R.drawable.homepage_normal);
        iv_tab_classification.setBackgroundResource(R.drawable.classification_normal);
        iv_tab_myself.setBackgroundResource(R.drawable.myself_selected);
        iv_tab_shopcart.setBackgroundResource(R.drawable.shopcart_normal);
    	iv_tab_service.setBackgroundResource(R.drawable.service_normal);
    	
    	tv_tab_home.setTextColor(getResources().getColor(R.color.tab_normal));
    	tv_tab_classification.setTextColor(getResources().getColor(R.color.tab_normal));
    	 tv_tab_myself.setTextColor(getResources().getColor(R.color.tab_selected));
    	 tv_tab_shopcart.setTextColor(getResources().getColor(R.color.tab_normal));
    	 tv_tab_service.setTextColor(getResources().getColor(R.color.tab_normal));
    	 
    	 index=3;
        ft.commit();
		
	}
	
	public void changeShopCart() {
		  FragmentTransaction    ft = mFM.beginTransaction();
		 hideFragments(ft);
		 if(shopCartFragment==null){
			 shopCartFragment=new ShopCartFragment();
			 ft.add(R.id.content_container, shopCartFragment);
		 }else {
			ft.show(shopCartFragment);
		}
        iv_tab_homepage.setBackgroundResource(R.drawable.homepage_normal);
        iv_tab_classification.setBackgroundResource(R.drawable.classification_normal);
        iv_tab_myself.setBackgroundResource(R.drawable.myself_normal);
        iv_tab_shopcart.setBackgroundResource(R.drawable.shopcart_selected);
    	iv_tab_service.setBackgroundResource(R.drawable.service_normal);
    	
    	tv_tab_home.setTextColor(getResources().getColor(R.color.tab_normal));
    	tv_tab_classification.setTextColor(getResources().getColor(R.color.tab_normal));
    	 tv_tab_myself.setTextColor(getResources().getColor(R.color.tab_normal));
    	 tv_tab_shopcart.setTextColor(getResources().getColor(R.color.tab_selected));
    	 tv_tab_service.setTextColor(getResources().getColor(R.color.tab_normal));
    	 index=4;
        ft.commit();
		
	}
	public void changeService() {
		  FragmentTransaction    ft = mFM.beginTransaction();
		 hideFragments(ft);
		 if(serviceFragment==null){
			 serviceFragment=new ServiceFragment();
			 ft.add(R.id.content_container, serviceFragment);
		 }else {
			ft.show(serviceFragment);
		}
	        iv_tab_homepage.setBackgroundResource(R.drawable.homepage_normal);
	        iv_tab_classification.setBackgroundResource(R.drawable.classification_normal);
	        iv_tab_myself.setBackgroundResource(R.drawable.myself_normal);
	        iv_tab_shopcart.setBackgroundResource(R.drawable.shopcart_normal);
	    	iv_tab_service.setBackgroundResource(R.drawable.service_selected);
	    	
	    	tv_tab_home.setTextColor(getResources().getColor(R.color.tab_normal));
	    	tv_tab_classification.setTextColor(getResources().getColor(R.color.tab_normal));
	    	 tv_tab_myself.setTextColor(getResources().getColor(R.color.tab_normal));
	    	 tv_tab_shopcart.setTextColor(getResources().getColor(R.color.tab_normal));
	    	 tv_tab_service.setTextColor(getResources().getColor(R.color.tab_selected));
	    	 index=5;
	        ft.commit();
			
		}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			
				if ((System.currentTimeMillis() - exitTime) > 2000) {
					ToastUtil.showShort(this, "再按一次退出");
					exitTime = System.currentTimeMillis();
				} else {
					APP.appExit();
				}
			
		}
		return true;
	}
}
	

